<?php
namespace app\admin\controller;

use app\admin\model\ComUserModel;
use think\Controller;

class Comuser extends Controller
{
    //用户列表
    public function index()
    {
        if(request()->isAjax()){

            $param = input('param.');

            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;

            $where = [];
            if (!empty($param['searchText'])) {
                $where['title'] = ['like', '%' . $param['searchText'] . '%'];
            }

            $article = new ComUserModel();
            $selectResult = $article->getUsersByWhere($where, $offset, $limit);

            foreach($selectResult as $key=>$vo){
                $selectResult[$key]['datetime'] = date('Y-m-d H:i:s',$vo['datetime']);
                $selectResult[$key]['photo'] = '<img src="' . $vo['photo'] . '" width="40px" height="40px">';

                $selectResult[$key]['role']==1? $selectResult[$key]['role']='普通用户' :$selectResult[$key]['role']= '商业用户';
                $selectResult[$key]['status'] = showOperate($this->status($vo['id'], $vo['status']));
//                $selectResult[$key]['operate'] = showOperate($this->makeButton($vo['id']));
            }

            $return['total'] = $article->getAllUsers($where);  // 总数据
            $return['rows'] = $selectResult;

            return json($return);
        }
        return $this->fetch();
    }

    //改变状态
    public function statusEdit()
    {
        $id = input('param.id');
        $s =input('param.status');
        $article = new ComUserModel();
        $flag = $article->editStatus($id,$s);
        return json(msg($flag['code'], $flag['data'], $flag['msg']));
    }
//状态按钮 1 为进行中 2为结束
    public function status($id,$s)
    {
        if ($s==1){
            return [
                '启用' => [
                    'auth' => 'role/roleedit',
                    'href' => "javascript:statusEdit(" . $id .",". $s.")",
                    'btnStyle' => 'info',
                    'icon' => 'fa fa-institution'
                ]
            ];
        }else{
            return [
                '禁用' => [
                    'auth' => 'role/roleedit',
                    'href' => "javascript:statusEdit(" . $id."," .$s. ")",//该按钮已失效
                    'btnStyle' => 'info',
                    'icon' => 'fa fa-institution'
                ]
            ];
        }
    }
}